<template>
  <div class="rescueRegist pd30 propertyMaintenance">
    <div class="panelWrap">
      <div class="title flex flexac flexjcsb fs14 cfff fwb pl p-r-20">
        <div>添加救援单位</div>
        <!-- <i class="el-icon-close" @click="handleCloseDialogUpdate"></i> -->
      </div>
      <div class="con">
        <div class="con-row flex flexac pl ">
          <div class="marginAuto flex flexac">
            <div class="inputLable inputLable1 flex flexac">
              <span class="name texttr"><span class="mustval">*</span>机构名称：</span>
              <el-input v-model="rescue.name" class="input" placeholder="请输入机构名称" />
            </div>
            <div class="inputLable inputLable2 flex flexac m-l-60">
              <span class="name texttr">机构简称：</span>
              <el-input v-model="rescue.nameSimple" class="input" placeholder="请输入机构简称" />
            </div>
          </div>
        </div>

        <div class="con-row flex flexac pl ">
          <div class="marginAuto flex flexac">

            <div class="inputLable inputLable1 flex flexac ">
              <span class="name texttr">坐标：</span>
              <el-input class="input coordinateRead" :value="getcoordinate" readonly placeholder="请选择坐标" />
              <el-button class="locationBtn" @click="choiceCoordinate=true">定位</el-button>
            </div>
            <div class="inputLable inputLable1 flex flexac  m-l-60">
              <span class="name texttr">救援站地址：</span>
              <el-input v-model="rescue.address" class="input" placeholder="请输入救援站地址" />
            </div>
          </div>
        </div>
        <div class="con-row flex flexac pl ">
          <div class="marginAuto flex flexac">
            <div class="inputLable inputLable1 flex flexac ">
              <span class="name texttr">救援站电话：</span>
              <el-input v-model="rescue.phone" class="input" placeholder="请输入救援站电话" />
            </div>
            <div class="inputLable inputLable2 flex flexac m-l-60">
              <span class="name texttr">邮政编码：</span>
              <el-input v-model="rescue.zipCode" class="input" placeholder="请输入邮政编码" />
            </div>
          </div>
        </div>
        <div class="con-row flex flexac pl ">
          <div class="marginAuto flex flexac">
            <div class="inputLable inputLable2 flex flexac ">
              <span class="name texttr"><span class="mustval">*</span>驻站人一：</span>
              <el-input v-model="rescue.stationPerson1" class="input" rescue placeholder="请输入驻站人一" />
            </div>
            <div class="inputLable inputLable1 flex flexac m-l-60">
              <span class="name texttr"><span class="mustval">*</span>驻站人一电话：</span>
              <el-input v-model="rescue.stationPersonPhone1" class="input" placeholder="请输入驻站人一电话" />
            </div>
          </div>
        </div>
        <div class="con-row flex flexac pl ">
          <div class="marginAuto flex flexac">
            <div class="inputLable inputLable2 flex flexac ">
              <span class="name texttr"><span class="mustval">*</span>驻站人二：</span>
              <el-input v-model="rescue.stationPerson2" class="input" rescue placeholder="请输入驻站人二" />
            </div>
            <div class="inputLable inputLable1 flex flexac m-l-60">
              <span class="name texttr"><span class="mustval">*</span>驻站人二电话：</span>
              <el-input v-model="rescue.stationPersonPhone2" class="input" placeholder="请输入驻站人二电话" />
            </div>
          </div>
        </div>
        <div class="con-row flex flexac pl ">
          <div class="marginAuto flex flexac">
            <div class="inputLable inputLable2 flex flexac ">
              <span class="name texttr"><span class="mustval">*</span>救援站负责人：</span>
              <el-input v-model="rescue.chargePerson" class="input" rescue placeholder="请输入救援站负责人" />
            </div>
            <div class="inputLable inputLable1 flex flexac m-l-60">
              <span class="name texttr"><span class="mustval">*</span>救援站负责人电话：</span>
              <el-input v-model="rescue.chargePersonPhone" class="input" placeholder="请输入救援站负责人电话" />
            </div>
          </div>
        </div>

        <div class="con-row flex flexac pl ">
          <div class="marginAuto flex flexac">

            <div class="inputLable inputLable2 flex flexac  ">
              <span class="name texttr">法人：</span>
              <el-input v-model="rescue.legalPerson" class="input" rescue placeholder="请输入法人" />
            </div>
          </div>
          <div class="inputLable inputLable1 flex flexac m-l-60">
            <span class="name texttr">法人电话：</span>
            <el-input v-model="rescue.legalPersonPhone" class="input" placeholder="请输入法人电话" />
          </div>
        </div>

        <div class="con-row flex flexac pl ">
          <div class="inputLable inputLable1 flex flexac ">
            <span class="name texttr">法人身份证：</span>
            <el-input v-model="rescue.legalPersonIdCard" class="input" placeholder="请输入法人身份证" />
          </div>
        </div>

        <div class="con-row flex flexac pl ">
          <div class="inputLable inputLable2 flex flexac ">
            <span class="name texttr">行政区域：</span>
            <city :region-code="rescue.regionCode" />
          </div>
        </div>

        <div class="con-row flex flexac pl ">
          <div class="inputLable wd100 inputLable1 flex ">
            <span class="name texttr">描述：</span>
            <textarea v-model="rescue.remark" class="markinfo" placeholder="请输入描述" />
          </div>
        </div>
      </div>

      <div class="flex  footerBtn m-t-35">
        <el-button class="defaultbtn saveBtn" @click="handleSave">保存</el-button>
        <el-button class="defaultbtn clsoeBtn" @click="handleCloseDialogUpdate">关闭</el-button>
      </div>
    </div>
    <el-dialog v-if="choiceCoordinate" custom-class="coordinateWrap" :close-on-click-modal="false" :visible.sync="choiceCoordinate">
      <coordinate />
    </el-dialog>

  </div>
</template>

<script>

import { rescueAdd } from '@/api/maintenance/role'
import { showNotice, successNotice, checkMobile } from '@/utils/toolclass'
import uploadImg from '@/components/uploadImg'
import city from '@/components/city'
import coordinate from '@/components/coordinate'
import bus from '@/utils/bus'

// import BmGeolocation  from 'vue-baidu-map/components/map/BmGeolocation.vue'
export default {
  components: {
    uploadImg, city, coordinate
  },
  props: {
    addFactory: {
      type: Object
    },
    isEdit: {
      type: Boolean
    }
  },
  data() {
    return {
      zoom: 10,
      rescue: {
        id: '',
        type: 0,
        name: '',
        nameSimple: '',

        address: '',
        phone: '',
        zipCode: '',

        longitude: '',
        latitude: '',

        stationPerson1: '',
        stationPersonPhone1: '',
        stationPerson2: '',
        stationPersonPhone2: '',
        chargePerson: '',
        chargePersonPhone: '',
        legalPerson: '',
        legalPersonPhone: '',
        legalPersonIdCard: '',
        remark: '',
        regionCode: {
          province: 430000, city: 430100, area: 430102
        }
      },
      choiceCoordinate: false

    }
  },
  computed: {
    getcoordinate: function() {
      if (this.rescue.longitude == '' || this.rescue.latitude == '') {
        return ''
      } else {
        return ('' + this.rescue.longitude) + ',' + (this.rescue.latitude + '')
      }
    }
  },
  created() {
    this.onCloseCoordinate()
    this.onSendCoordinate()
  },
  methods: {
    onSendCoordinate() {
      bus.$on('sendCoordinate', (data) => {
        this.rescue.longitude = data.lng
        this.rescue.latitude = data.lat
        this.handleCloseCoordinate()
      })
      bus.$on('sendCoordinateAddress', (data) => {
        // console.log('sendCoordinateAddress')
        // console.log(data)
        this.rescue.address = (data.province === undefined ? '' : data.province) + '' + (data.city === undefined ? '' : data.city) + '' + (data.title === undefined ? '' : data.title)
      })
    },
    onCloseCoordinate() {
      bus.$on('closeCoordinate', (data) => {
        this.handleCloseCoordinate()
      })
    },
    handleCloseCoordinate() {
      this.choiceCoordinate = false
    },
    handleImginfo(res) {
    },
    handleUpdate() {
      simpleViewUpdate(this.addFactory).then((res) => {
        if (res.code == '000000') {
          showNotice('success', '提示', '保存成功')
          this.$emit('handleRefresh')
          this.$emit('handleCloseDialog')
        }
      })
    },
    handleSave() {
      if (this.rescue.name == '') {
        showNotice('error', '提示', '机构名称为空')
        return
      }
      if (this.rescue.stationPerson1 == '') {
        showNotice('error', '提示', '驻站人一为空')
        return
      }
      if (!checkMobile(this.rescue.stationPersonPhone1)) {
        showNotice('error', '提示', '驻站人一电话手机格式不对')
        return
      }
      if (this.rescue.stationPerson2 == '') {
        showNotice('error', '提示', '驻站人二为空')
        return
      }
      if (!checkMobile(this.rescue.stationPersonPhone2)) {
        showNotice('error', '提示', '驻站人二电话手机格式不对')
        return
      }
      if (this.rescue.chargePerson == '') {
        showNotice('error', '提示', '救援站负责人为空')
        return
      }
      if (!checkMobile(this.rescue.chargePersonPhone)) {
        showNotice('error', '提示', '救援站负责人电话手机格式不对')
        return
      }
      this.handlerescueAdd(this.rescue)
    },
    handleCloseDialog() {
      this.$emit('handleCloseDialog')
    },
    handleCloseDialogUpdate() {
      this.$emit('handleRefresh')
      this.$emit('handleCloseDialog')
    },
    handlerescueAdd(obj) {
      rescueAdd(obj).then((res) => {
        if (res.code == '000000') {
          showNotice('success', '提示', '保存成功!')
          this.$emit('handleRefresh')
          this.$emit('handleCloseDialog')
        }
      })
    }
  }
}
</script>

<style rel='stylesheet/scss' lang='scss' scoped>
@import "@/styles/addtable.scss";
.inputLable .name {
  width: 140px !important;
  letter-spacing: 0px;
}
</style>
